import api from '@/api';
import { message } from 'ant-design-vue';
import { useMessage } from '@/hooks/common/useMessage';
import { useRouter } from 'vue-router';
import { deleteApiObj } from '@/config/table';
import { unref } from 'vue';
export const useTableAction = (tableNo: string, getList?: () => void, relativeChangeTableRef?: any) => {
    const router = useRouter();
    // 详情
    const showDetail = (id: number) => {
        let path = '';
        switch (tableNo) {
            case 'taskRequirements': path = '/taskRequire/taskDetail'; break;
            case 'productRequirements': path = '/productRequire/productDetail'; break;
            case 'version':
            case 'productAnalysisVersion': path = '/version/versionDetail'; break;
            case 'productAnalysis': path = '/productAnalysis/productDetail'; break;
            case 'requireChange': path = '/requireChange/changeDetail'; break;
            case 'system': path = '/setting/system/systemDetail'; break;
        }
        router.push({
            path,
            query: {
                id
            }
        });
    };
    // 变更需求
    const handleChangeRequire = (id: number) => {
        const { createConfirm } = useMessage();
        createConfirm({
            title: '发起需求变更后将不可操作该需求，确定要发起需求变更？',
            iconType: 'danger',
            onOk: async () => {
                const query = {
                    id,
                    type: tableNo === 'productRequirements' ? 'product' : 'business'
                };
                const { success, errorMsg } = await api.requireChangeModel.requireChange(query);
                if (success) {
                    message.success('操作成功');
                    router.push({
                        path: '/requireChange/requireChangeList'
                    });
                } else {
                    message.error(errorMsg);
                }
            }
        });
    };
    // 显示需求变更
    const showRequireChangeDetail = (id: number, type) => {
        unref(relativeChangeTableRef).open({
            id,
            type
        });
    };
    // 删除
    const handleDelete = (id: number, title: string, fn: Function = () => { }) => {
        const { createConfirm } = useMessage();
        createConfirm({
            title: `确认删除【${title}】?`,
            iconType: 'danger',
            onOk: async () => {
                const query = {
                    id
                };
                const { success, errorMsg } = await deleteApiObj[tableNo](query);
                if (success) {
                    message.success('删除成功');
                    getList && getList();
                    if (fn) {
                        fn(id);
                    }
                } else {
                    message.error(errorMsg);
                }
            }
        });
    };

    return {
        showDetail,
        handleChangeRequire,
        showRequireChangeDetail,
        handleDelete
    };
};